package com.kuang.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 朱文胜
 * @Date: 2022/10/12/16:57
 * @Description:
 * 线程池配置类
 *
 *
 * =======  =====  ==== 那个方法使用多线程就再方法上加 @Async("myexecutor")
 * ===========================     myexecutor一般为方法名  如果@Bean起了名字
 *                                                就是bean的名字
 *
 *
 */
@Configuration
@EnableAsync
public class threadPollConfig {

    @Bean
    public Executor myexecutor(){
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //设置核心线程数
        executor.setCorePoolSize(24);
        //设置最大线程数
        executor.setMaxPoolSize(64);
        //设置等待队列
        executor.setQueueCapacity(5);
        //设置拒绝策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        //设置线程池的名称
        executor.setThreadNamePrefix("thread");
        //初始化
        executor.initialize();

        return executor;
    }
}
